capture program drop generateIF
program generateIF
	syntax varlist(max=1),  [mean variance quantile(real .5)]
	
	local Y `varlist'
	
	
	*storing statistics of the outcome distribution
 	quietly sum `Y' if !missing(Y)
	local Ybar=r(mean) 
	local Yvar=r(Var)  
 	*taking empirical support as support of Y
	local ylower= r(min)
	local yupper= r(max)

	*generating influence functions and their upper and lower bounds
	if "`mean'" == "mean" {
		dis in green "Estimating bounds on conditional average treatment effects on the"
		dis in yellow " mean"
		quietly gen IF= `Y' - `Ybar' if !missing(Y)
		quietly gen IFlower =`ylower' - `Ybar' if !missing(Y)
		quietly gen IFupper=`yupper' - `Ybar' if !missing(Y)

	}
	else if "`variance'"=="variance" {
		dis in green "Estimating bounds on conditional average treatment effects on the"
		dis in yellow "variance"
		quietly gen IF=`Y'^2 - 2* `Ybar'*`Y' - `Yvar' + `Ybar'^2 if !missing(Y)
		quietly gen IFlower = - `Yvar' if !missing(Y)
		quietly gen IFupper = max(`ylower'^2 - 2* `Ybar'*`ylower' - `Yvar' + `Ybar'^2, `yupper'^2 - 2* `Ybar'*`yupper' - `Yvar' + `Ybar'^2    ) if !missing(Y)
	}
	else if "`quantile'" != ""{
		dis in green "Estimating bounds on conditional average treatment effects on the"
		dis in yellow "`quantile'th quantile"
		local tau = `quantile'*100
		_pctile `Y', p(`tau')
		local Yquantile= r(r1)
		quietly gen yqtmp = `Yquantile'
		kdensity `Y', at(yqtmp) gen(yquan ydens)  nograph
		drop yqtmp yquan
		quietly gen IF = - ((`Y'<= `Yquantile') - `quantile')/ ydens if !missing(Y)
		quietly gen IFlower =  - (1 - `quantile')/ ydens if !missing(Y)
		quietly gen IFupper =   (`quantile')/ ydens if !missing(Y)
		drop ydens
	}
	else {
		dis in red "must specify option mean, variance, or quantile(tau)"
	}
	
end


